import {useStore} from "vuex";
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import {SingletonState} from "../../interface";
import ground from "@/views/mobile/mobile_ground.vue"

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    redirect:{
      name:"社团广场"
    }
  },
  {
    path: '/search',
    name: '搜索',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/mobile/mobile_search.vue')
  },
  {
    path: '/ground',
    name: '社团广场',
    meta:{
      title:"社团广场"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/mobile/mobile_ground.vue')
  },
  {
    path: '/owner',
    name: '我的',
    meta: {
      requireAuth: true // 配置此条，进入页面前判断是否需要登陆
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/mobile/mobile_tools.vue'),
    children: [
      {
        path: "",//子路由前不需要加斜杠 /
        name: "owner",
        component: () => import('../views/mobile/mobile_owner.vue'),
        props: true,
        meta: { title: "查看申请" },
      },
      {
        path: "viewapply",//子路由前不需要加斜杠 /
        name: "查看申请",
        component: () => import('../views/mobile/mobile_view_apply.vue'),
        props: true,
        meta: { title: "查看申请" },
      },
      {
        path: "viewjoined",//子路由前不需要加斜杠 /
        name: "查看加入的社团",
        component: () => import('../views/mobile/mobile_view_joined.vue'),
        props: true,
        meta: { title: "查看加入的社团" },
      },
      {
        path: "viewnotify",//子路由前不需要加斜杠 /
        name: "通知",
        component: () => import('../views/mobile/notify/mobile_view_notify.vue'),
        props: true,
        meta: { title: "查看加入的社团" },
      },
      {
        path: "viewclubforms",//子路由前不需要加斜杠 /
        name: "查看社团的所有表单",
        component: () => import('../views/mobile/mobile_club_forms.vue'),
        props: true,
        meta: { title: "查看表单" },
      },
      {
        path: "/manageclub",//子路由前不需要加斜杠 /
        name: "管理单个社团",
        component: () => import('../views/mobile/mobile_manage_club.vue'),
        props: true,
        meta: { title: "管理社团" },
      },
      {
        path: "createjsonpage",//子路由前不需要加斜杠 /
        name: "创建社团详情页面json版本",
        component: () => import('../views/mobile/mobile_create_club_json_page.vue'),
        props: true,
        meta: { title: "创建社团详情页面json版本" },
      },
      {
        path: "passnewpeople",
        name: "通过入社审批",
        component: () => import('../views/mobile/president/mobile_new_pass.vue'),
        props: true,
        meta: {title: "通过入社审批"}
      }
    ]
  },
  // {
  //   path: "/owner/viewapply",//子路由前不需要加斜杠 /
  //   name: "apply",
  //   component: () => import('../views/mobile/mobile_view_apply.vue'),
  //   props: true,
  //   meta: { title: "查看申请" },
  // },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/mobile/mobile_login.vue')
  },
  {
    path: '/register',
    name: 'register',
    component:()=>import('../views/mobile/mobile_register.vue')
  },
  {
    path: '/makeform',
    name: '制作社团表单',
    component:()=>import('../views/mobile/form/mobile_makeform.vue'),
    meta: {
      requireAuth: true // 配置此条，进入页面前判断是否需要登陆
    },
  },
  {
    path: '/manage',
    name: '选择管理社团',
    component:()=>import('../views/mobile/mobile_view_manage.vue'),
    meta: {
      requireAuth: true // 配置此条formview，进入页面前判断是否需要登陆
    },
  },
  {
    path: '/form',
    name: '提交表单',
    component:()=>import('../views/mobile/form/mobile_form.vue'),
    meta: {
      requireAuth: true // 配置此条，进入页面前判断是否需要登陆
    },
  },
  {
    path: '/formview',
    name: '查看表单',
    component:()=>import('../views/mobile/form/mobile_viewform.vue'),
    meta: {
      requireAuth: true // 配置此条，进入页面前判断是否需要登陆
    },
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})



// const store = useStore();
// console.log(store);
router.beforeEach((to,from,next)=>{

  if (to.matched.some(res => res.meta.requireAuth)) { // 验证是否需要登陆

    if (sessionStorage['login'] == SingletonState.logined) { // 查询本地存储信息是否已经登陆
      next();
    } else {
      next({
        path: '/login', // 未登录则跳转至login页面
        query: {redirect: to.fullPath} // 登陆成功后回到当前页面，这里传值给login页面，to.fullPath为当前点击的页面
      });
    }
  } else {
    next();
  }
})
export default router
